<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;
use App\Domain\Vip as Domain_Vip;

class User extends NotORM {
	
	/* 用户列表 */
	public function getUserList($uid,$p,$type){
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
		
		$where="user_type=2";
		if($type==1){
			$where.=" and id !=?";
		}else if($type==2){
			$where.=" and isauthor_auth=0 and id !=?";
		}else{
			$where.=" and isauthor_auth=1 and id !=?";
		}

		$list=\PhalApi\DI()->notorm->user
					->select("id,user_nickname,avatar,avatar_thumb,sex,isauth,online,consumption,votestotal,isauthor_auth")
					->where($where,$uid)
					->limit($start,$pnum)
                    ->order('online desc,create_time desc')
					->fetchAll();
		
		$domain_vip = new Domain_Vip();		
		foreach($list as $k=>$v){
			$v['avatar']=\App\get_upload_path($v['avatar']);
			$v['avatar_thumb']=\App\get_upload_path($v['avatar_thumb']);
			if($v['isauthor_auth']==0){
				$v['level']=\App\getLevel($v['consumption']);
			}else{
				$v['level']=\App\getLevelanchor($v['votestotal']);
			}			
			$v['u2t']= \App\isAttention($uid,$v['id']);
			$vipinfo=$domain_vip->myVip($uid);
            $v['isvip']=$vipinfo['isvip'];
			$v['isblack']=\App\isBlack($uid,$v['id']);
			$list[$k]=$v;
	   }
		
		return $list;
	}
	
	/* 打招呼文案信息 */
	public function getSayhelloinfo(){
		$info="你好~";
		$list=\PhalApi\DI()->notorm->user_say_hello
					->select("*")					
                    ->order('addtime desc')
					->fetchAll();
					
		//处理随机获取一条文案信息
		if($list){
			$nums=count($list)-1;
			if($nums>0){
				$i = rand(0,$nums);//随机生成一个0，到总数之间的整形数字
				$info=$list[$i]['content'];
			}else{
				$info=$list[0]['content'];
			}
		}
	
		return $info;
	}
	
	/* 推荐信息 */
	public function getRecommendlist($p,$uid=''){
        if($p<1){
            $p=1;
        }
        $where = '';
        if(!empty($uid)){
            $where = 'id <> '.$uid;
        }
		$pnum=3;
		$start=($p-1)*$pnum;
		$list=\PhalApi\DI()->notorm->user
					->select("id,user_nickname,avatar,avatar_thumb,sex,isauth,isauthor_auth")
					->where('user_type=2 and isauthor_auth=1')
					->where($where)
					->limit($start,$pnum)
                    ->order('create_time desc')
					->fetchAll();
					
	   foreach($list as $k=>$v){
			$v['avatar']=\App\get_upload_path($v['avatar']);
			$v['avatar_thumb']=\App\get_upload_path($v['avatar_thumb']);
			//处理成给每个人的打招呼信息都是随机
			// $sayhelloinfo=$this->getSayhelloinfo();
			// $v['sayhelloinfo']=$sayhelloinfo;
			$list[$k]=$v;
	   }
		
		return $list;
	}

	/* 用户全部信息 */
	public function getBaseInfo($uid) {
		$info=\PhalApi\DI()->notorm->user
				->select("id,user_nickname,avatar,avatar_thumb,sex,signature,coin,votes,consumption,votestotal,birthday,goodnums,badnums,isauth,isvoice,voice_value,isvideo,video_value,isdisturb,isauthor_auth,audio,audio_length,height,weight,constellation,labelid,label,label_c,province,city,district,intr,user_login,mobile,country,country_code,language,address")
				->where('id=?  and user_type=2',$uid)
				->fetchOne();
		if(!empty($info['country'])){
		    $country=\PhalApi\DI()->notorm->country
				->select("*")
				->where('code',$info['country'])
				->where('type',0)
				->fetchOne();
    		$country['name_str'] = json_decode($country['name_str'],true);
    		$country['img'] = \App\get_upload_path($country['img']);
    		$info['country'] = $country;
		}
		
		if(!empty($info['language'])){
		    $language=\PhalApi\DI()->notorm->country
				->select("*")
				->where('code',$info['language'])
				->where('type',1)
				->fetchOne();
    		$language['name_str'] = json_decode($language['name_str'],true);
    		$info['language'] = $language;
		}
		
		return $info;
	}
    /* 语音价格 */
    public function getVoice() {

		$list=\PhalApi\DI()->notorm->fee_voice
				->select('coin,level')
                ->order('coin asc')
				->fetchAll();

        return $list; 
    }
    /* 视频价格 */
    public function getVideo() {

		$list=\PhalApi\DI()->notorm->fee_video
				->select('coin,level')
                ->order('coin asc')
				->fetchAll();
                
        return $list; 
    }
    /* 检测用户昵称 */
    public function checkNickname($uid,$name){
        $isexist=\PhalApi\DI()->notorm->user
				->select("id")
				->where('id!=?  and user_nickname=?',$uid,$name)
				->fetchOne();
        if($isexist){
            return 1;
        }
        
        return 0;
    }
    
    /* 关注列表 */
	public function getFollowsList($uid,$p){
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
        
		$list=\PhalApi\DI()->notorm->user_attention
					->select("touid")
					->where('uid=?',$uid)
					->limit($start,$pnum)
                    ->order('addtime desc')
					->fetchAll();
		
		return $list;
	}
    
	/* 粉丝列表 */
	public function getFansList($uid,$p){
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
		$list=\PhalApi\DI()->notorm->user_attention
					->select("uid")
					->where('touid=?',$uid)
					->limit($start,$pnum)
                    ->order('addtime desc')
					->fetchAll();
		
		return $list;
	}
    
    /* 关注 */
	public function setAttent($uid,$touid){
        \PhalApi\DI()->notorm->user_attention
            ->insert(array("uid"=>$uid,"touid"=>$touid,"addtime"=>time()));
        return 1;		 
	}
    
    /* 取消关注 */
	public function delAttent($uid,$touid){
        \PhalApi\DI()->notorm->user_attention
            ->where('uid=? and touid=?',$uid,$touid)
            ->delete();
        return 0;
	}

	/* 拉黑列表 */
	public function getBlackList($uid,$p){
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
		$list=\PhalApi\DI()->notorm->user_black
					->select("touid")
					->where('uid=?',$uid)
					->limit($start,$pnum)
                    ->order('addtime desc')
					->fetchAll();
		
		return $list;
	}
    
    /* 拉黑 */
	public function setBlack($uid,$touid){
        \PhalApi\DI()->notorm->user_black
            ->insert(array("uid"=>$uid,"touid"=>$touid,"addtime"=>time()));
        return 1;		 
	}
    
    /* 取消拉黑 */
	public function delBlack($uid,$touid){
        \PhalApi\DI()->notorm->user_black
            ->where('uid=? and touid=?',$uid,$touid)
            ->delete();
        return 0;
	}

    /* 用户信息更新 */
	public function upUserInfo($uid,$data){
        $rs=0;
        if($data){
            $rs=\PhalApi\DI()->notorm->user
                ->where('id=? ',$uid)
                ->update($data);
        }
        
        return $rs;
	}

    /* 礼物柜 */
	public function getGiftCab($liveuid){
        $rs=[
            'total'=>'0',
            'nums'=>'0',
            'list'=>[],
        ];
        
        $total=\PhalApi\DI()->notorm->user_coinrecord
                ->select("sum(nums) as total_nums,sum(totalcoin) as total_coin")
                ->where('type=0 and action=1 and touid=? ',$liveuid)
                ->fetchOne();
        if($total){
            if($total['total_nums']){
                $rs['nums']=$total['total_nums'];
            }
            if($total['total_coin']){
                $rs['total']=$total['total_coin'];
            }
        }
                
        $list=\PhalApi\DI()->notorm->user_coinrecord
                ->select("actionid,sum(nums) as total_nums")
                ->where('type=0 and action=1 and touid=? ',$liveuid)
                ->group('actionid')
                ->order('total_nums desc')
                ->fetchAll();
        $rs['list']=$list;
        
        return $rs;
	}
    
	/* 个人主页 */
	public function getUserHome($uid) {
		$info=\PhalApi\DI()->notorm->user
				->select("id,user_nickname,avatar,avatar_thumb,birthday,goodnums,badnums,isauth,isvoice,voice_value,isvideo,video_value,isdisturb,online,last_online_time,votestotal,isauthor_auth,audio,audio_length,height,weight,constellation,labelid,label,label_c,province,city,district,intr,signature,sex,consumption,country,language")
				->where('id=?  and user_type=2',$uid)
				->fetchOne();
		if(!empty($info['country'])){
		    $country=\PhalApi\DI()->notorm->country
				->select("*")
				->where('code',$info['country'])
				->where('type',0)
				->fetchOne();
    		$country['name_str'] = json_decode($country['name_str'],true);
    		$country['img'] = \App\get_upload_path($country['img']);
    		$info['country'] = $country;
		}
		
		if(!empty($info['language'])){
		    $language=\PhalApi\DI()->notorm->country
				->select("*")
				->where('code',$info['language'])
				->where('type',1)
				->fetchOne();
    		$language['name_str'] = json_decode($language['name_str'],true);
    		$info['language'] = $language;
		}
		return $info;
	}

	/* 接听 */
	public function getAnswerRate($uid) {
		$info=\PhalApi\DI()->notorm->answer_rate
				->select("*")
				->where('uid=? ',$uid)
				->fetchOne();
		return $info;
	}
	
	
	/* 用户举报-类型 */
	public function getReportClass(){
		return  \PhalApi\DI()->notorm->user_report_classify
                    ->select("*")
					->order("list_order asc")
					->fetchAll();
	}
	
	
	/* 用户举报 */
	public function setReport($uid,$touid,$content){
		return  \PhalApi\DI()->notorm->user_report
				->insert(array("uid"=>$uid,"touid"=>$touid,'content'=>$content,'addtime'=>time() ) );	
	}
	
	/* 美颜预设 */
	public function setBeauty($uid,$type,$preinstall){
		
		$data=array(
			"uid"=>$uid,
			'time'=>time()
		); 
		if($type==1){  //默认
			$data['moren']=$preinstall;
		}else if($type==2){  //美狐
			$data['meihu']=$preinstall;
		}else{
			return 0;
		}
		
		
		
		$result=\PhalApi\DI()->notorm->user_beauty_preinstall
            ->where('uid=?',$uid)
            ->update($data);
		if(!$result){
			$result=\PhalApi\DI()->notorm->user_beauty_preinstall
				->insert($data);	
		}
		return  $result;
	}
	
	/* 获取美颜预设 */
	public function getBeauty($uid){
		
		
		//定义空数据
		$lists['moren']=[
			'type'=>'1',
			'ishave'=>'0',
			'preinstall'=>(object)[],
		];
		$lists['meihu']=[
			'type'=>'2',
			'ishave'=>'0',
			'preinstall'=>(object)[],
		];


		
		//开始查询
		$info=\PhalApi\DI()->notorm->user_beauty_preinstall
				->select("*")
				->where('uid=?',$uid)
				->fetchOne();
		if($info){
			if($info['moren']!=''){
				$lists['moren']=[
					'type'=>'1',
					'ishave'=>'1',
					'preinstall'=>json_decode($info['moren'],true),
				];
			}
			if($info['meihu']!=''){
				$lists['meihu']=[
					'type'=>'2',
					'ishave'=>'1',
					'preinstall'=>json_decode($info['meihu'],true),
				];
			}
		}
		return  $lists;
	}

	/* 形象标签 */   	
    public function getLabelList() {

		$list=\PhalApi\DI()->notorm->label
				->select('id,name,name_en,colour')
                ->order('list_order asc')
				->fetchAll(); 
		$lang=\PhalApi\DI()->lang;
		foreach($list as $k=>$v){
		    //ggg
			/*if($lang=='en'){
				$v['name']=$v['name_en'];
			}*/
			$list[$k]=$v;
		}
        return $list; 
    }

    //
    public function getUserMaterial($uid){
    	$userinfo=\PhalApi\DI()->notorm->user
    		->select('id,user_nickname,avatar,avatar_thumb,audio,audio_length,sex,height,weight,constellation,labelid,label,label_c,province,city,district,intr,signature,language,country,address')
    		->where(['user_type'=>2,'id'=>$uid])
    		->fetchOne();

    	return $userinfo;

    }

    public function checkTeenager($uid){
		$rs=array('code'=>0,'msg'=>'','info'=>array());
		
		$info=\PhalApi\DI()->notorm->user_teenager
			->where(['uid'=>$uid])
			->fetchOne();

		if(!$info){

			$arr=['is_setpassword'=>'0','status'=>'0'];
			$rs['info'][0]=$arr;

			return $rs;
		}

		$arr=['is_setpassword'=>'1','status'=>(string)$info['status']];
		$rs['info'][0]=$arr;

		return $rs;
	}


	//检测用户青少年模式是否可用
	public function checkTeenagerIsOvertime($uid){
		$rs=array('code'=>0,'msg'=>'','info'=>array());

		$now=time();

		$hour=date("H",$now);

		//测试用$hour=23;

		if($hour>22 || $hour<6){
			$rs['code']=10010; //code固定
			$rs['msg']=\PhalApi\T('青少年模式下每日晚22时至次日6时期间无法使用APP');
			return $rs;
		}

		$info = \PhalApi\DI()->notorm->user_teenager_time
			->where(['uid'=>$uid])
			->fetchOne();

		//测试用$info['length']=2500;

		if($info){

			if($info['length'] >= 40*60){
				$rs['code']=10011; //code固定
				$rs['msg']=\PhalApi\T('青少年模式下你今日的使用时长已超过40分钟，不能继续使用APP');
				return $rs;
			}
		}

		return $rs;
	}


	public function setTeenagerPassword($uid,$password,$type){
		$info=\PhalApi\DI()->notorm->user_teenager
			->where(['uid'=>$uid])
			->fetchOne();

		$password=md5($password);

		if($info){

			if($type==1){ //**开启青少年模式

				if($password != $info['password']){
					return 1001;
				}
			}
			
			$res=\PhalApi\DI()->notorm->user_teenager
				->where(['uid'=>$uid])
				->update(
					[
						'edittime'=>time(),
						'status'=>1
					]
				);

		}else{

			//新增记录
			$res=\PhalApi\DI()->notorm->user_teenager
				->where(['uid'=>$uid])
				->insert(['uid'=>$uid,'password'=>$password,'status'=>1,'addtime'=>time()]);
		}

		if(!$res){
			return 1002;
		}

		return 1;

	}


	public function updateTeenagerPassword($uid,$oldpassword,$password){

		$info=\PhalApi\DI()->notorm->user_teenager
			->where(['uid'=>$uid])
			->fetchOne();

		if(!$info){
			return 1001;
		}

		if(md5($oldpassword) != $info['password']){
			return 1002;
		}

		$res = \PhalApi\DI()->notorm->user_teenager
			->where(['uid'=>$uid])
			->update(
				[
					'password'=>md5($password),
					'edittime'=>time()
				]
			);


		return $res;


	}

	public function closeTeenager($uid,$password){
		$info=\PhalApi\DI()->notorm->user_teenager
			->where(['uid'=>$uid])
			->fetchOne();

		if(!$info){
			return 1001;
		}

		if(md5($password) != $info['password']){
			return 1003;
		}

		if(!$info['status']){
			return 1002;
		}

		$res=\PhalApi\DI()->notorm->user_teenager
			->where(['uid'=>$uid])
			->update(
				[
					'status'=>0,
					'edittime'=>time()
				]
			);

		return $res;
	}

	//定时增加用户青少年模式使用时间
	public function addTeenagerTime($uid){

		$rs=array('code'=>0,'msg'=>\PhalApi\T('更新成功'),'info'=>array());

		$info=\PhalApi\DI()->notorm->user_teenager
			->where(['uid'=>$uid])
			->fetchOne();

		$msg=\PhalApi\T('用户未开启青少年模式');

		if(!$info){
			$rs['code']=1001;
			$rs['msg']=$msg;
			return $rs;
		}

		if(!$info['status']){
			$rs['code']=1002;
			$rs['msg']=$msg;
			return $rs;
		}


		$res = $this->checkTeenagerIsOvertime($uid);

		if($res['code']!=0){
			return $res;
		}

		$now=time();

		$info = \PhalApi\DI()->notorm->user_teenager_time
			->where(['uid'=>$uid])
			->fetchOne();

		if(!$info){
			\PhalApi\DI()->notorm->user_teenager_time->insert(['uid'=>$uid,'length'=>10,'addtime'=>$now]);
		}else{

			\PhalApi\DI()->notorm->user_teenager_time->where(['uid'=>$uid])
				->update(
					array(
						'length' => new \NotORM_Literal("length + 10"),
						'uptime'=>$now
					)
				);
		}

		return $rs;
	}


	/* 登录奖励信息 */
	public function LoginBonus($uid){
		$rs=array(
			'bonus_switch'=>'0',
			'bonus_day'=>'0',
			'count_day'=>'0',
			'bonus_list'=>array(),
			'coin'=>'0'
		);
        

		$configpri=\App\getConfigPri();
		if(!$configpri['bonus_switch']){
			return $rs;
		}
		$rs['bonus_switch']=$configpri['bonus_switch'];


		/* 获取登录设置 */
        $key='loginbonus';
		$list=\App\getcaches($key);
		if(!$list){
            $list=\PhalApi\DI()->notorm->loginbonus
					->select("day,coin")
					->fetchAll();
			if($list){
				\App\setcaches($key,$list);
			}
		}
        

		
		$bonus_coin=array();
		foreach($list as $k=>$v){
			$bonus_coin[$v['day']]=$v['coin'];
			$list[$k]['day']=(string)$v['day'];
			$list[$k]['coin']=(string)$v['coin'];
		}

		$rs['bonus_list']=$list;

		/* 登录奖励 */
		$signinfo=\PhalApi\DI()->notorm->user_sign
					->select("bonus_day,bonus_time,count_day")
					->where('uid=?',$uid)
					->fetchOne();


		if(!$signinfo){
			$signinfo=array(
				'bonus_day'=>'0',
				'bonus_time'=>'0',
				'count_day'=>'0',
			);
        }

        $nowtime=time();
        if($nowtime - $signinfo['bonus_time'] > 60*60*24){
            $signinfo['count_day']=0;
        }


        $rs['count_day']=(string)$signinfo['count_day'];

		
		if($nowtime>$signinfo['bonus_time']){
			//**更新
			$bonus_time=strtotime(date("Ymd",$nowtime))+60*60*24;
			$bonus_day=$signinfo['bonus_day'];
			if($bonus_day>6){
				$bonus_day=0;
			}
			$bonus_day++;
            $coin=$bonus_coin[$bonus_day];

            
			if($coin){
                $rs['bonus_day']=(string)$bonus_day;
                $rs['coin']=(string)$coin;
            }
			
		}else{
			
			//今天已签到
			$bonus_day = $signinfo['bonus_day'];
			foreach ($list as $k => $v) {
				if($v['day']==$bonus_day){
					$rs['coin']=(string)$v['coin'];
					break;
				}
			}
		}

		return $rs;
	}

	/* 获取登录奖励 */
	public function getLoginBonus($uid){
		$rs=0;
		$configpri=\App\getConfigPri();
		if(!$configpri['bonus_switch']){
			return $rs;
		}
		
		/* 获取登录设置 */
        $key='loginbonus';
		$list=\App\getcaches($key);
		if(!$list){
            $list=\PhalApi\DI()->notorm->loginbonus
					->select("day,coin")
					->fetchAll();
			if($list){
				\App\setcaches($key,$list);
			}
		}

		$bonus_coin=array();
		foreach($list as $k=>$v){
			$bonus_coin[$v['day']]=$v['coin'];
		}
		
		$isadd=0;
		/* 登录奖励 */
		$signinfo=\PhalApi\DI()->notorm->user_sign
					->select("bonus_day,bonus_time,count_day")
					->where('uid=?',$uid)
					->fetchOne();
		if(!$signinfo){
			$isadd=1;
			$signinfo=array(
				'bonus_day'=>'0',
				'bonus_time'=>'0',
				'count_day'=>'0',
			);
        }
		$nowtime=time();
		if($nowtime>$signinfo['bonus_time']){
			//**更新
			$bonus_time=strtotime(date("Ymd",$nowtime))+60*60*24;
			$bonus_day=$signinfo['bonus_day'];
			$count_day=$signinfo['count_day'];
			if($bonus_day>6){
				$bonus_day=0;
			}
            if($nowtime - $signinfo['bonus_time'] > 60*60*24){
                $count_day=0;
            }
			$bonus_day++;
			$count_day++;
            
 
            if($isadd){
                \PhalApi\DI()->notorm->user_sign
                    ->insert(
                    	array(
                    		"uid"=>$uid,
                    		"bonus_time"=>$bonus_time,
                    		"bonus_day"=>$bonus_day,
                    		"count_day"=>$count_day
                    	)
                    );

            }else{
                \PhalApi\DI()->notorm->user_sign
                    ->where('uid=?',$uid)
                    ->update(
                    	array(
                    		"bonus_time"=>$bonus_time,
                    		"bonus_day"=>$bonus_day,
                    		"count_day"=>$count_day
                    	)
                    );
            }
            
            $coin=$bonus_coin[$bonus_day];
            
			if($coin){
                \App\addCoin($uid,$coin);
				
                // 记录
                $insert=array(
                	"type"=>'1',
                	"action"=>'12', //签到
                	"uid"=>$uid,
                	"touid"=>$uid,
                	"actionid"=>$bonus_day,
                	"nums"=>'0',
                	"totalcoin"=>$coin,
                	"showid"=>'0',
                	"addtime"=>$nowtime
                );

                \App\addCoinRecord($insert);
            }
            $rs=1;
		}
		
		return $rs;
		
	}

	//获取新手任务
	public function getUserTask($uid,$type){
		$info=\PhalApi\DI()->notorm->user_task_list
			->where(['uid'=>$uid,'type'=>$type])
			->fetchOne();

		return $info;
	}

	//修改新手任务
	public function changeUserTask($uid,$type){
		$info=\PhalApi\DI()->notorm->user_task_list
			->where(['uid'=>$uid,'type'=>$type])
			->fetchOne();

		$configpri=\App\getConfigPri();

		$coin=$configpri[$type.'_coin'];


		if(!$info){

			\PhalApi\DI()->notorm->user_task_list
				->insert(
					[
						'uid'=>$uid,
						'type'=>$type,
						'coin'=>$coin,
						'addtime'=>time()
					]
				);
		}

		return 1;
	}

	public function checkDailyTasks($uid,$type){

		$time=strtotime(date("Y-m-d 00:00:00",time()));

		$data=[
			'id'=>'0',
			'state'=>'0',
		];

		$configpri=\App\getConfigPri();

		$target=$configpri[$type.'_term'];
		$reward=$configpri[$type.'_coin'];

		$save=[
			'uid'=>$uid,
			'type'=>$type,
			'target'=>$target,
			'schedule'=>'0',
			'reward'=>$reward,
			'addtime'=>$time,
			'state'=>'0'
		];

		$where="uid={$uid} and type='{$type}'";

		$info=\PhalApi\DI()->notorm->user_daily_tasks
			->where($where)
			->select("*")
			->fetchOne();

		if(!$info){
			$info=\PhalApi\DI()->notorm->user_daily_tasks->insert($save);
		}else if($info['addtime']!=$time){
			$save['uptime']=time(); //更新时间
			\PhalApi\DI()->notorm->user_daily_tasks->where("id={$info['id']}")->update($save);
		}else{
			$data['state']=$info['state'];
		}

		$data['id']=$info['id'];

		return $data;

	}

	public function receiveTaskReward($uid,$type){
		$rs = array('code' => 0, 'msg' => '', 'info' => array());

		$where=['uid'=>$uid,'type'=>$type];

		$info=\PhalApi\DI()->notorm->user_daily_tasks
			->where($where)
			->fetchOne();

		if(!$info){
			$rs['code']='1001';
			$rs['msg']=\PhalApi\T('系统繁忙,请稍后操作');
			return $rs;
		}

		if($info['state']==0){
			$rs['code']='1001';
			$rs['msg']=\PhalApi\T('任务未达标,请继续加油');
		}else if($info['state']==2){
			$rs['code']='1001';
			$rs['msg']=\PhalApi\T('奖励已送达,不能重复领取!');
		}else{
			$rs['msg']=\PhalApi\T('奖励已发放,明天继续加油');

			$issave=\PhalApi\DI()->notorm->user_daily_tasks
				->where(['id'=>$info['id']])
				->update(['state'=>2,'uptime'=>time()]);

			if($issave){

				$coin=$info['reward'];
				$isprofit =\App\addCoin($uid,$coin);

				if($isprofit){

					$action_arr=[
						'publish_dynamic'=>17,
						'like_dynamic'=>18,
						'send_privatemsg'=>19,
						'voice_call'=>20,
						'video_call'=>21,
						'open_live'=>22
					];

					$insert=[
						'type'=>1,
						'action'=>$action_arr[$type],
						'uid'=>$uid,
						'touid'=>$uid,
						'totalcoin'=>$coin,
						'addtime'=>time()
					];
					\App\addCoinRecord($insert);
				}

			}else{
				$rs['code']='1001';
				$rs['msg']=\PhalApi\T('领取失败,请稍后再试');
			}
		}

		return $rs;
	}


	//消费(土豪)榜
	public function guardList($where='',$p=''){
		 if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
		
		$result=\PhalApi\DI()->notorm->user_coinrecord
            ->select('sum(totalcoin) as totalcoin, uid')
            ->where($where)
            ->group('uid')
            ->order('totalcoin desc')
            ->limit($start,$pnum)
            ->fetchAll();
		return $result;
		
	}

	public function getRandomSayhi($uid){
		$info=\PhalApi\DI()->notorm->sayhi
		->order("rand()")
    	->limit(0,1)
    	->fetchOne();

    	if(!$info){
    		$info=array(
    			'id'=>0,
    			'list_order'=>1,
    			'name'=>\PhalApi\T('你好'),
    			'name_en'=>\PhalApi\T('你好')
    		);
    	}
		
		$lang=\PhalApi\DI()->lang;

		if($lang=='en'){
			$info['name']=$info['name_en'];
		}

		return $info['name'];
	}

	public function addFirstSayhiRecord($uid,$touid){
		$info=\PhalApi\DI()->notorm->sayhi_record
			->where(['uid'=>$uid,'touid'=>$touid])
			->fetchOne();

		if($info){
			return 0;
		}

		\PhalApi\DI()->notorm->sayhi_record->insert(['uid'=>$uid,'touid'=>$touid,'addtime'=>time()]);

		return 1;
	}


	public function getSayhiRecord($uid,$touid){

		$info=\PhalApi\DI()->notorm->sayhi_record
		->where(['uid'=>$uid,'touid'=>$touid])
		->fetchOne();

		if(!$info){
			return '1';
		}

		$begin=strtotime(date("Y-m-d",time()));

		if($info['addtime']<$begin){
			\PhalApi\DI()->notorm->sayhi_record
			->where(['uid'=>$uid,'touid'=>$touid])
			->delete();

			return '1';
		}

		return '0';
	}


	/*public function getLikeNums($uid){
		$nums = \PhalApi\DI()->notorm->sayhi_record
			->where(['uid'=>$uid])
			->count();

		return $nums;
	}*/
	
	public function getLikeNums($uid){
		$nums = \PhalApi\DI()->notorm->dynamics_like
	        ->alias('L')
		    ->leftJoin('dynamics', 'D', 'L.dynamicid = D.id')
			->where(['L.uid'=>$uid])
			->where('L.status',1)
			->count();

		return $nums;
	}

	public function getLikeList($uid,$p){
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
		$list=\PhalApi\DI()->notorm->sayhi_record
					->select("touid as uid")
					->where('uid=?',$uid)
					->limit($start,$pnum)
                    ->order('addtime desc')
					->fetchAll();
		
		return $list;
	}
	
}
